﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using MbUnit.Framework;
using System;
using WatiN.Core;
using System.IO;

namespace DotNetNuke.Tests.UI.WatiN.Common
{
    public class WatiNAssert
    {
        private IE _ieInstance;
        private string _screenShotPartialPath;

        public WatiNAssert(IE ieInstance, string screenShotPartialPath)
        {
            IeInstance = ieInstance;
            ScreenShotPartialPath = screenShotPartialPath;
        }

        public IE IeInstance
        {
            get { return _ieInstance; }
            set { _ieInstance = value;}
        }

        public string ScreenShotPartialPath
        {
            get { return _screenShotPartialPath; }
            set { _screenShotPartialPath = value; }
        }

        public void AssertIsTrue(bool check, string screenShotFileName)
        {
            if (!check)
            {
                if (File.Exists(ScreenShotPartialPath + screenShotFileName))
                {
                    File.Delete(ScreenShotPartialPath + screenShotFileName);
                }
                System.Threading.Thread.Sleep(1500);
                IeInstance.CaptureWebPageToFile(ScreenShotPartialPath + screenShotFileName);
            }
            Assert.IsTrue(check);
        }

        public void AssertIsTrue(bool check, string screenShotFileName, IE screenShotWindow)
        {
            if (!check)
            {
                if (File.Exists(ScreenShotPartialPath + screenShotFileName))
                {
                    File.Delete(ScreenShotPartialPath + screenShotFileName);
                }
                screenShotWindow.CaptureWebPageToFile(ScreenShotPartialPath + screenShotFileName);
            }
            Assert.IsTrue(check);
        }

        public void AssertIsTrue(bool check, string screenShotFileName, string message)
        {
            if (!check)
            {
                if (File.Exists(ScreenShotPartialPath + screenShotFileName))
                {
                    File.Delete(ScreenShotPartialPath + screenShotFileName);
                }
                IeInstance.CaptureWebPageToFile(ScreenShotPartialPath + screenShotFileName);
            }
            Assert.IsTrue(check, message);
        }

        public void AssertIsFalse(bool check, string screenShotFileName)
        {
            if (check)
            {
                if (File.Exists(ScreenShotPartialPath + screenShotFileName))
                {
                    File.Delete(ScreenShotPartialPath + screenShotFileName);
                }
                IeInstance.CaptureWebPageToFile(ScreenShotPartialPath + screenShotFileName);
            }
            Assert.IsFalse(check);
        }

        public void AssertIsFalse(bool check, string screenShotFileName, IE screenShotWindow)
        {
            if (check)
            {
                if (File.Exists(ScreenShotPartialPath + screenShotFileName))
                {
                    File.Delete(ScreenShotPartialPath + screenShotFileName);
                }
                screenShotWindow.CaptureWebPageToFile(ScreenShotPartialPath + screenShotFileName);
            }
            Assert.IsFalse(check);
        }


        public void AssertIsFalse(bool check, string screenShotFileName, string message)
        {
            if (check)
            {
                if (File.Exists(ScreenShotPartialPath + screenShotFileName))
                {
                    File.Delete(ScreenShotPartialPath + screenShotFileName);
                }
                IeInstance.CaptureWebPageToFile(ScreenShotPartialPath + screenShotFileName);
            }
            Assert.IsFalse(check, message);
        }

        public void AssertStringsAreEqual(string stringOne, string stringTwo, string screenShotFileName)
        {
            if (!stringOne.Equals(stringTwo))
            {
                if (File.Exists(ScreenShotPartialPath + screenShotFileName))
                {
                    File.Delete(ScreenShotPartialPath + screenShotFileName);
                }
                IeInstance.CaptureWebPageToFile(ScreenShotPartialPath + screenShotFileName);
            }
            Assert.AreEqual(stringOne, stringTwo);
        }

        public static void ContentContains<TElement>(string expectedSubString, TElement element) where TElement : global::WatiN.Core.Element
        {
            string message = String.Format("Element - {0} did not contain expected substring - {1}.  Actual Value of element's InnerHtml - {2}",
                                            element.Id, expectedSubString, element.InnerHtml);
            ContentContains<TElement>(expectedSubString, element,message);
        }

        public static void ContentContains<TElement>(string expectedSubString, TElement element, string message) where TElement : global::WatiN.Core.Element
        {
            Assert.Contains(element.InnerHtml, expectedSubString, message);
        }

        public static void ContentEquals<TElement>(string expectedValue, TElement element) where TElement : global::WatiN.Core.Element
        {
            string message = String.Format("Element - {0} did not equal the string - {1}.  Actual Value of element's InnerHtml - {2}",
                                            element.Id, expectedValue, element.InnerHtml);
            ContentEquals<TElement>(expectedValue, element , message);
        }

        public static void ContentEquals<TElement>(string expectedValue, TElement element, string message) where TElement : global::WatiN.Core.Element
        {
            Assert.Contains(element.InnerHtml, expectedValue, message);
        }

        public static void ElementExists<TElement>(TElement element) where TElement : global::WatiN.Core.Element
        {
            string message = String.Format("Could not find the element that was being searchd for.");
            ElementExists<TElement>(element, message);
        }

        public static void ElementExists<TElement>(TElement element, string message) where TElement : global::WatiN.Core.Element
        {
            Assert.IsTrue(element.Exists, message);
        }

        public static void ElementDoesNotExists<TElement>(TElement element) where TElement : global::WatiN.Core.Element
        {
            string message = String.Format("The following element existed when it shouldn't have:{0}", element.Id);
            ElementExists<TElement>(element, message);
        }

        public static void ElementDoesNotExists<TElement>(TElement element, string message) where TElement : global::WatiN.Core.Element
        {
            Assert.IsFalse(element.Exists, message);
        }

    }
}
